package system

import (
	"context"

	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/system"
	systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
)

type SysPatientsService struct{}

// CreateSysPatients 创建患者记录
// Author [yourname](https://github.com/yourname)
func (sysPatientsService *SysPatientsService) CreateSysPatients(ctx context.Context, sysPatients *system.SysPatients) (err error) {

	err = global.GVA_DB.Create(sysPatients).Error
	return err
}

// DeleteSysPatients 删除患者记录
// Author [yourname](https://github.com/yourname)
func (sysPatientsService *SysPatientsService) DeleteSysPatients(ctx context.Context, ID string) (err error) {
	err = global.GVA_DB.Delete(&system.SysPatients{}, "id = ?", ID).Error
	return err
}

// DeleteSysPatientsByIds 批量删除患者记录
// Author [yourname](https://github.com/yourname)
func (sysPatientsService *SysPatientsService) DeleteSysPatientsByIds(ctx context.Context, IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]system.SysPatients{}, "id in ?", IDs).Error
	return err
}

// UpdateSysPatients 更新患者记录
// Author [yourname](https://github.com/yourname)
func (sysPatientsService *SysPatientsService) UpdateSysPatients(ctx context.Context, sysPatients system.SysPatients) (err error) {
	err = global.GVA_DB.Model(&system.SysPatients{}).Where("id = ?", sysPatients.ID).Updates(&sysPatients).Error
	return err
}

// GetSysPatients 根据ID获取患者记录
// Author [yourname](https://github.com/yourname)
func (sysPatientsService *SysPatientsService) GetSysPatients(ctx context.Context, ID string) (sysPatients system.SysPatients, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&sysPatients).Error
	return
}

// GetSysPatientsInfoList 分页获取患者记录
// Author [yourname](https://github.com/yourname)
func (sysPatientsService *SysPatientsService) GetSysPatientsInfoList(ctx context.Context, info systemReq.SysPatientsSearch, storeId ...int) (list []system.SysPatients, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&system.SysPatients{})
	var sysPatientss []system.SysPatients
	// 如果有条件搜索 下方会自动创建搜索语句
	if len(info.CreatedAtRange) == 2 {
		db = db.Where("created_at BETWEEN ? AND ?", info.CreatedAtRange[0], info.CreatedAtRange[1])
	}
	if len(storeId) > 0 {
		db = db.Where("store_id = ?", storeId[0])
	}

	if info.PatientName != "" {
		db = db.Where("patient_name LIKE ?", "%"+info.PatientName+"%")
	}

	if info.Phone != "" {
		db = db.Where("phone LIKE ?", "%"+info.Phone+"%")
	}

	if info.IdCardNumber != "" {
		db = db.Where("id_card_number LIKE ?", "%"+info.IdCardNumber+"%")
	}

	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&sysPatientss).Error
	return sysPatientss, total, err
}
func (sysPatientsService *SysPatientsService) GetSysPatientsPublic(ctx context.Context) {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
